/**
 * 神州信息(2012)
 */
package net.abbp.report.model.headerfooter.io;

import org.dom4j.Element;


import net.abbp.core.transform.xml.jaxb.ColorAdapter;
import net.abbp.core.util.BooleanUtils;
import net.abbp.core.util.NumberUtils;
import net.abbp.core.util.StringUtils;
import net.abbp.report.model.cell.style.FontStyle;
import net.abbp.report.model.headerfooter.TextHFElement;
import net.abbp.report.model.io.IValueIO;
import net.abbp.report.utils.ColorUtils;

/**
 * TextHFElement写入写出器
 * 
 * @version 1.0
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ABBP 1.0
 * 
 *        <pre>
 * 历史：
 *      建立: Jul 29, 2012 lexloo
 * </pre>
 */
public class TextHFElementIO implements IValueIO {

    @Override
    public Object read(Element value) {
        TextHFElement textHF = new TextHFElement();
        readFromXml(value, textHF);

        return textHF;
    }

    @Override
    public void write(Element element, Object value) {
        TextHFElement textHF = (TextHFElement) value;

        writeToXml(element, textHF);
    }

    /**
     * 写入到XML
     * 
     * @param element Xml Element
     * @param textHF 文本元素
     */
    public void writeToXml(Element element, TextHFElement textHF) {
        Element elText = element.addElement("Text").addText(textHF.getText());
        FontStyle fontStyle = textHF.getFontStyle();

        Element elFont = element.addElement("FontStyle");
        elFont.addAttribute("Name", fontStyle.getName());
        elFont.addAttribute("Style", StringUtils.convertToString(fontStyle.getStyle(), "0"));
        elFont.addAttribute("Size", StringUtils.convertToString(fontStyle.getSize(), "12"));
        elFont.addAttribute("Foreground", ColorAdapter.toHexEncoding(fontStyle.getForeground()));
        elFont.addAttribute("Shadow", StringUtils.convertToString(fontStyle.isShadow(), "false"));
        elFont.addAttribute("Strikethrough", StringUtils.convertToString(fontStyle.isStrikethrough(), "false"));
        elFont.addAttribute("Subscript", StringUtils.convertToString(fontStyle.isSubscript(), "false"));
        elFont.addAttribute("Superscript", StringUtils.convertToString(fontStyle.isSuperscript(), "false"));
        elFont.addAttribute("Underline", StringUtils.convertToString(fontStyle.getUnderline(), ""));
    }

    /**
     * 从XML读入到对象
     * 
     * @param element Xml Element
     * @param textHF 文本元素
     */
    public void readFromXml(Element element, TextHFElement textHF) {
        textHF.setText(element.elementText("Text"));

        Element elFontStyle = element.element("FontStyle");
        FontStyle fontStyle = FontStyle.getInstance();
        fontStyle = fontStyle.applyName(elFontStyle.attributeValue("Name"));
        fontStyle = fontStyle.applyStyle(NumberUtils.convertToInteger(elFontStyle.attributeValue("Style"), 0));
        fontStyle = fontStyle.applySize(NumberUtils.convertToInteger(elFontStyle.attributeValue("Size"), 12));
        fontStyle = fontStyle.applyForeground(ColorUtils.parseToColor(elFontStyle.attributeValue("Foreground")));
        fontStyle = fontStyle.applyShadow(BooleanUtils.parseString(elFontStyle.attributeValue("Shadow")));
        fontStyle = fontStyle.applyStrikethrough(BooleanUtils.parseString(elFontStyle.attributeValue("Strikethrough")));
        fontStyle = fontStyle.applySubscript(BooleanUtils.parseString(elFontStyle.attributeValue("Subscript")));
        fontStyle = fontStyle.applySuperscript(BooleanUtils.parseString(elFontStyle.attributeValue("Superscript")));
        fontStyle = fontStyle.applyUnderline(NumberUtils.convertToInteger(elFontStyle.attributeValue("Underline"), 0));

        textHF.setFontStyle(fontStyle);
    }
}
